package com.loveofsoftware.fotolab.effects;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

public class TintInitialEffect implements InitialEffects {

	int deg = 40;

	public Bitmap transform(Bitmap in) {

		int width, height;
		height = in.getHeight();
		width = in.getWidth();

		in = in.copy(Config.ARGB_8888, true);
		int[] pix = new int[width * height];
		in.getPixels(pix, 0, width, 0, 0, width, height);

		int RY, /*GY,*/ BY, RYY, GYY, BYY, R, G, B, Y;
		double angle = (3.14159d * (double) deg) / 180.0d;
		int S = (int) (256.0d * Math.sin(angle));
		int C = (int) (256.0d * Math.cos(angle));

		for (int y = 0; y < height; y++)
			for (int x = 0; x < width; x++) {
				int index = y * width + x;
				int r = (pix[index] >> 16) & 0xff;
				int g = (pix[index] >> 8) & 0xff;
				int b = pix[index] & 0xff;
				RY = (70 * r - 59 * g - 11 * b) / 100;
				//GY = (-30 * r + 41 * g - 11 * b) / 100;
				BY = (-30 * r - 59 * g + 89 * b) / 100;
				Y = (30 * r + 59 * g + 11 * b) / 100;
				RYY = (S * BY + C * RY) / 256;
				BYY = (C * BY - S * RY) / 256;
				GYY = (-51 * RYY - 19 * BYY) / 100;
				R = Y + RYY;
				R = (R < 0) ? 0 : ((R > 255) ? 255 : R);
				G = Y + GYY;
				G = (G < 0) ? 0 : ((G > 255) ? 255 : G);
				B = Y + BYY;
				B = (B < 0) ? 0 : ((B > 255) ? 255 : B);
				pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
			}
		in.setPixels(pix, 0, width, 0, 0, width, height);
		pix = null;
		return in;
	}
}
